/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package progweb.project.Servlet;

import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.log4j.*;

/**
 *
 * @author michele
 */
public class LoginServLet extends HttpServlet {
    private static org.apache.log4j.Logger log=Logger.getLogger(LoginServLet.class);
    Object user;
    String type;
    HttpSession sessione;
    
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        
        RequestDispatcher rd;
        this.sessione=request.getSession();
        //Controllo che non sia stato chiesto un logout
        String sLogout = request.getParameter("logout");
        if(sLogout!=null){ 
            log.info("Richiesta di logout");
            if(sLogout.equals("yes") && this.sessione!=null){
                this.sessione.invalidate();
                log.info("Sessione invalidata");
            }
            rd=request.getRequestDispatcher("/index.html");
            rd.forward(request, response);
        }
        else {
            
            /*leggo parametri dalla sessione*/
            log.info("Lettura parametri dalla sessione");
            this.user = (Object) this.sessione.getAttribute("user");
            this.type = (String) this.sessione.getAttribute("type");
            if(this.user==null || this.type==null){
                log.error("Sessione inesistente o scaduta");
                request.setAttribute("error", "Errore: sessione inesistente o scaduta");
                request.setAttribute("page", "index.html");
                rd=request.getRequestDispatcher("/ErrorServlet");
                rd.forward(request, response);
            }
            
            log.info("Indirizzamento alla Welcome");
            /*se medico forwardo id alla servlet WelcomeServletSL*/
            if(this.type.equals("medico")){
                log.debug("User type = medico");
                rd=request.getRequestDispatcher("/MedicoWelcomeSL");
                rd.forward(request, response);
            }
            /*se paziente forwardo id alla servlet WelcomePazienteSL*/
            else if(this.type.equals("paziente")){
                log.debug("User type = paziente");
                rd=request.getRequestDispatcher("/PazienteWelcomeSL");
                rd.forward(request, response);
            }
        }
    }
    
    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
